<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>MongoClient 类</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.core.html">核心类</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongoclient.close.html">MongoClient::close</a></div>
 <div class="up"><a href="mongo.core.html">核心类</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="class.mongoclient" class="reference">

 <h1 class="title">MongoClient 类</h1>
 

 <div class="partintro"><p class="verinfo">(PECL mongoclient &gt;=1.3.0)</p>
  
  <div class="section" id="mongoclient.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    PHP 和 MongoDB 之间的连接。
   </p>
   <p class="para">
    这个类用于创建和管理连接。典型的用法：
    <div class="example" id="mongoclient.intro-example">
     <p><strong>Example #1 <strong class="classname">MongoClient</strong> 基本用法</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;连接<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;获取名称为&nbsp;"foo"&nbsp;的数据库<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    关于创建连接的更多信息，参见 <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span> 和 <a href="mongo.connecting.html" class="link">connecting</a> 的章节。
   </p>
  </div>
  

  <div class="section" id="mongoclient.synopsis">
   <h2 class="title">类摘要</h2>

   
   <div class="classsynopsis">
    <div class="ooclass"></div>

    
    <div class="classsynopsisinfo">
     <span class="ooclass">
      <strong class="classname">MongoClient</strong>
     </span>
     {</div>
    

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 常量 */</div>
    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.version"><var class="varname">VERSION</var></a></var>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.defaulthost"><var class="varname">DEFAULT_HOST</var></a></var>
     <span class="initializer"> = &quot;localhost&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.defaultport"><var class="varname">DEFAULT_PORT</var></a></var>
     <span class="initializer"> = 27017</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.rpprimary"><var class="varname">RP_PRIMARY</var></a></var>
     <span class="initializer"> = &quot;primary&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.rpprimarypreferred"><var class="varname">RP_PRIMARY_PREFERRED</var></a></var>
     <span class="initializer"> = &quot;primaryPreferred&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.rpsecondary"><var class="varname">RP_SECONDARY</var></a></var>
     <span class="initializer"> = &quot;secondary&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.rpsecondary"><var class="varname">RP_SECONDARY_PREFERRED</var></a></var>
     <span class="initializer"> = &quot;secondaryPreferred&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongoclient.html#mongoclient.constants.rpsecondary"><var class="varname">RP_NEAREST</var></a></var>
     <span class="initializer"> = &quot;nearest&quot;</span>
    ;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 属性 */</div>
    <div class="fieldsynopsis">
     <span class="modifier">public</span>
     <span class="type">boolean</span>
      <var class="varname"><a href="class.mongoclient.html#mongoclient.props.connected">$<var class="varname">connected</var></a></var>
     <span class="initializer"> = <strong><code>FALSE</code></strong></span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">public</span>
     <span class="type">string</span>
      <var class="varname"><a href="class.mongoclient.html#mongoclient.props.status">$<var class="varname">status</var></a></var>
     <span class="initializer"> = <strong><code>NULL</code></strong></span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">protected</span>
     <span class="type">string</span>
      <var class="varname">$<var class="varname">server</var></var>
     <span class="initializer"> = <strong><code>NULL</code></strong></span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">protected</span>
     <span class="type">boolean</span>
      <var class="varname">$<var class="varname">persistent</var></var>
     <span class="initializer"> = <strong><code>NULL</code></strong></span>
    ;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="mongoclient.construct.html" class="methodname">__construct</a></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$server</code><span class="initializer"> = &quot;mongodb://localhost:27017&quot;</span></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = array(&quot;connect&quot; =&gt; <strong><code>TRUE</code></strong>)</span></span>
  ]] )</div>

    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongoclient.close.html" class="methodname">close</a></span>
    ([ <span class="methodparam"><span class="type"><span class="type boolean|string">boolean|string</span></span> <code class="parameter">$connection</code></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongoclient.connect.html" class="methodname">connect</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongoclient.dropdb.html" class="methodname">dropDB</a></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$db</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">MongoDB</span><span class="methodname"><a href="mongoclient.get.html" class="methodname">__get</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$dbname</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">void</span><span class="methodname"><a href="mongoclient.getconnections.html" class="methodname">getConnections</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongoclient.gethosts.html" class="methodname">getHosts</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongoclient.getreadpreference.html" class="methodname">getReadPreference</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongoclient.killcursor.html" class="methodname">killCursor</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$server_hash</code></span>
   , <span class="methodparam"><span class="type"><span class="type int|MongoInt64">int|MongoInt64</span></span> <code class="parameter">$id</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span><span class="methodname"><a href="mongoclient.listdbs.html" class="methodname">listDBs</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">MongoCollection</span><span class="methodname"><a href="mongoclient.selectcollection.html" class="methodname">selectCollection</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$db</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$collection</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">MongoDB</span><span class="methodname"><a href="mongoclient.selectdb.html" class="methodname">selectDB</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span><span class="methodname"><a href="mongoclient.setreadpreference.html" class="methodname">setReadPreference</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$read_preference</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$tags</code></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">string</span><span class="methodname"><a href="mongoclient.tostring.html" class="methodname">__toString</a></span>
    ( <span class="methodparam">void</span>
   )</div>

   }</div>
   

  </div>


  <div class="section" id="mongoclient.constants">
   <h2 class="title">预定义常量</h2>
   <div class="section" id="mongoclient.constants.types">
    <h2 class="title">MongoClient 常量</h2>
    <dl>

     
      <dt id="mongoclient.constants.version"><strong><code>MongoClient::VERSION</code></strong></dt>

      <dd>

       <span class="simpara">
        PHP 驱动版本。有可能附加 &quot;+&quot; 或 &quot;-&quot; 如果是在两个版本之间。
       </span>
      </dd>

     
     
      <dt id="mongoclient.constants.defaulthost"><strong><code>MongoClient::DEFAULT_HOST</code></strong></dt>

      <dt>
<strong><code>&quot;localhost&quot;</code></strong></dt>

      <dd>

       <span class="simpara">
        如果没有指定主机，默认连接该主机。
       </span>
      </dd>

     
     
      <dt id="mongoclient.constants.defaultport"><strong><code>MongoClient::DEFAULT_PORT</code></strong></dt>

      <dt>
<strong><code>27017</code></strong></dt>

      <dd>

       <span class="simpara">
        如果没有指定端口，默认连接该端口。
       </span>
      </dd>

     
     
      <dt id="mongoclient.constants.rpprimary"><strong><code>MongoClient::RP_PRIMARY</code></strong></dt>

      <dt>
<strong><code>&quot;primary&quot;</code></strong></dt>

      <dd>

       <span class="simpara">
        副本集活跃节点的<a href="mongo.readpreferences.html" class="link">读取选项</a>。
       </span>
      </dd>

     
     
      <dt id="mongoclient.constants.rpprimarypreferred"><strong><code>MongoClient::RP_PRIMARY_PREFERRED</code></strong></dt>

      <dt>
<strong><code>&quot;primaryPreferred&quot;</code></strong></dt>

      <dd>

       <span class="simpara">
        副本集活跃节点的<a href="mongo.readpreferences.html" class="link">读取选项</a>。
       </span>
      </dd>

     
     
      <dt id="mongoclient.constants.rpsecondary"><strong><code>MongoClient::RP_SECONDARY</code></strong></dt>

      <dt>
<strong><code>&quot;secondary&quot;</code></strong></dt>

      <dd>

       <span class="simpara">
        副本集备份节点的<a href="mongo.readpreferences.html" class="link">读取选项</a>。
       </span>
      </dd>

     
     
      <dt id="mongoclient.constants.rpsecondarypreferred"><strong><code>MongoClient::RP_SECONDARY_PREFERRED</code></strong></dt>

      <dt>
<strong><code>&quot;secondaryPreferred&quot;</code></strong></dt>

      <dd>

       <span class="simpara">
        副本集备份节点的<a href="mongo.readpreferences.html" class="link">读取选项</a>。
       </span>
      </dd>

     
     
      <dt id="mongoclient.constants.rpnearest"><strong><code>MongoClient::RP_NEAREST</code></strong></dt>

      <dt>
<strong><code>&quot;nearest&quot;</code></strong></dt>

      <dd>

       <span class="simpara">
        副本集最近节点的<a href="mongo.readpreferences.html" class="link">读取选项</a>。
       </span>
      </dd>

     
    </dl>

   </div>
  </div>

  <div class="section" id="mongoclient.fields">
   <h2 class="title">字段属性</h2>
   <dl>

    
     <dt id="mongoclient.props.connected"><var class="varname"><var class="varname">connected</var></var></dt>

     <dd>

      <span class="simpara">
       如果我们有一个打开的、基于读取选项和标记集（对于集群连接）的数据库连接，将会被设置为 <strong><code>TRUE</code></strong>，否则是 <strong><code>FALSE</code></strong>。
       这个属性不考虑账户是否已认证。
      </span>
     </dd>

    
    
     <dt id="mongoclient.props.status"><var class="varname"><var class="varname">status</var></var></dt>

     <dd>

      <span class="simpara">
       是否为持久连接，创建的连接能够在此对象中复用。如果不是持久连接，这个字段会是 <strong><code>NULL</code></strong>。
      </span>
     </dd>

    
   </dl>

  </div>

  <div class="section" id="mongoclient.seealso">
   <h2 class="title">参见</h2>
   <ul class="simplelist">
    <li class="member"><a href="mongo.readpreferences.html" class="xref">Read Preferences</a></li>
    <li class="member"><a href="mongo.writeconcerns.html" class="xref">Write Concerns</a></li>
    <li class="member"><a href="mongo.connecting.html" class="xref">链接服务器</a></li>
    <li class="member">关于 <a href="http://docs.mongodb.org/manual/reference/connection-string/" class="link external">&raquo;&nbsp;connecting</a> 的 MongoDB 核心文档 </li>
   </ul>
  </div>


 </div>

 





 







 















 







 































 







 







 







 
















 



<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="mongoclient.close.html">MongoClient::close</a> — 关闭连接</li><li><a href="mongoclient.connect.html">MongoClient::connect</a> — 连接到数据库服务器</li><li><a href="mongoclient.construct.html">MongoClient::__construct</a> — 创建一个新的数据库连接对象</li><li><a href="mongoclient.dropdb.html">MongoClient::dropDB</a> — 删除一个数据库 [已废弃]</li><li><a href="mongoclient.get.html">MongoClient::__get</a> — 取得一个数据库</li><li><a href="mongoclient.getconnections.html">MongoClient::getConnections</a> — 返回所有已打开连接的信息</li><li><a href="mongoclient.gethosts.html">MongoClient::getHosts</a> — 更新所有关联主机的状态信息</li><li><a href="mongoclient.getreadpreference.html">MongoClient::getReadPreference</a> — 获取此连接的读取首选项</li><li><a href="mongoclient.killcursor.html">MongoClient::killCursor</a> — Kills a specific cursor on the server</li><li><a href="mongoclient.listdbs.html">MongoClient::listDBs</a> — 列出所有有效数据库</li><li><a href="mongoclient.selectcollection.html">MongoClient::selectCollection</a> — 获取数据库的文档集</li><li><a href="mongoclient.selectdb.html">MongoClient::selectDB</a> — 获取一个数据库</li><li><a href="mongoclient.setreadpreference.html">MongoClient::setReadPreference</a> — 为该连接设置读取选项</li><li><a href="mongoclient.tostring.html">MongoClient::__toString</a> — 该连接的字符串表达方式</li></ul>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.core.html">核心类</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongoclient.close.html">MongoClient::close</a></div>
 <div class="up"><a href="mongo.core.html">核心类</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
